package cn.gson.crm.controller.custom;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Employee;

/**
 * 处理分配的ajax servlet
 */
@WebServlet("/custom/assignajax")
public class CustomAssignAjaxServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private DeptDao deptDao = new DeptDao();
	private EmpDao empDao = new EmpDao();

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		System.out.println(type);
		if ("sondept".equals(type)) {
			String bigdeptid = request.getParameter("bigdeptid");
			List<Dept> data = deptDao.getSonDeptByUpdateId(bigdeptid);
			response.setContentType("application/json;charset=utf-8");

			// 把结果转换成JSON字符串
			String jsonStr = JSONArray.toJSONString(data);
			// 输出结果
			response.getWriter().append(jsonStr);
		}
		if ("area".equals(type)) {
			String sondeptid = request.getParameter("sondeptid");
			List<Employee> data = empDao.getEmpListByDeptId(sondeptid);
//			if (data != null) {
//				for (Employee employee : data) {
//					//System.out.println(employee.getEmpName() + "   " + employee.getEmpId());
//				}
//			}

			response.setContentType("application/json;charset=utf-8");
			// 把结果转换成JSON字符串
			String jsonStr = JSONArray.toJSONString(data);
			// 输出结果
			//System.out.println(jsonStr);
			response.getWriter().append(jsonStr);
		}

	}

}
